<template>
  <div class="register">
    <div class="register-form">
      <div>
        <h3>求职者注册</h3>
      </div>
      <el-form
        :model="addUserForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="70px"
      >
        <el-form-item label="用户名" prop="userName">
          <el-input
            v-model="addUserForm.userName"
            @blur="checkoutUser(addUserForm.userName)"
          ></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="realName">
          <el-input v-model="addUserForm.realName"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addUserForm.password" show-password></el-input>
        </el-form-item>

        <el-form-item label="电话" prop="phone">
          <el-input v-model="addUserForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addUserForm.email"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <!-- <template>
                            <el-select v-model="addUserForm.sex" placeholder="请选择">
                             <el-option
                                v-for="item in options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                            </el-option>
                            </el-select>
                        </template> -->
          <template>
            <el-radio-group v-model="addUserForm.sex">
              <el-radio :label="1">男</el-radio>
              <el-radio :label="0">女</el-radio>
            </el-radio-group>
          </template>
        </el-form-item>
        <el-row>
          <el-button type="success" class="zhuche" @click="getlist"
            >注册</el-button
          >
          <el-button type="info" class="zhuche" @click="backlogin"
            >已有账号</el-button
          >
          <el-button type="primary" class="chongzhi" @click="resetUserForm"
            >重置</el-button
          >
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    //验证邮箱的规则
    var checkEmail = (rule, value, cb) => {
      const regEmail = /^\w+@\w+(\.\w+)+$/;
      if (regEmail.test(value)) {
        return cb();
      }
      //返回一个错误提示
      cb(new Error("请输入合法的邮箱"));
    };
    //验证电话号码的规则
    var checkMobile = (rule, value, cb) => {
      const regMobile = /^1[3|4|5|7|8][0-9]{9}$/;
      if (regMobile.test(value)) {
        return cb();
      }
      //返回一个错误提示
      cb(new Error("请输入合法的手机号码"));
    };
    return {
      //表单验证规则
      addFormRules: {
        //验证用户名是否合法
        userName: [
          { required: true, message: "请输入登录名称", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "长度在 2 到 10 个字符",
            trigger: "blur",
          },
        ],
        //验证姓名是否合法
        realName: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "长度在 2 到 10 个字符",
            trigger: "blur",
          },
        ],
        //验证密码是否合法
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
        phone: [
          {
            required: true,
            message: "请输入手机号码",
            trigger: "blur",
          },
          {
            validator: checkMobile,
            message: "手机号码不正确, 请重新输入",
            trigger: "blur",
          },
        ],
        email: [
          {
            required: true,
            message: "请输入邮箱",
            trigger: "blur",
          },
          {
            validator: checkEmail,
            message: "邮箱格式不正确，请重新输入",
            trigger: "blur",
          },
        ],
      },
      // 性别的选择
      options: [
        {
          value: "男",
          label: "男",
        },
        {
          value: "女",
          label: "女",
        },
      ],
      value: "",
      //用户注册表单
      addUserForm: {
        userName: "",
        realName: "",
        password: "",
        sex: 1,
        phone: "",
        email: "",
      },
      // 用户名数组
      userArr: [],
    };
  },
  methods: {
    backlogin() {
      this.$router.push("/login");
    },
    //点击重置按钮，重置登录表单
    resetUserForm() {
      this.$refs.addFormRef.resetFields();
    },
    getlist() {
      console.log(this.addUserForm);
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post(
          "/userRegister",
          this.addUserForm
        );
        if (res.code !== 205)
          return this.$message.error("注册失败!此用户名已被注册");
        this.$message.success("注册成功！");
        this.$refs.addFormRef.resetFields();
      });
    },
    // 检查用户名是否被注册
    checkoutUser(userName) {
      let str = this.userArr.join(",");
      let reg = new RegExp(userName, "g");
      if (reg.test(str)) {
        this.$message.error("用户名不可用");
      } else {
        this.$message.success("用户名可用");
      }
      console.log(str);
      console.log(userName);
    },
  },
  async created() {
    const { data: res } = await this.$http.post("/getAllUserName");
    this.userArr = res;
    console.log(res);
  },
};
</script>

<style lang="less" scoped>
.register {
  background-color: aquamarine;
  height: 753px;
  position: relative;
  .register-form {
    width: 400px;
    height: 500px;
    background-color: #eee;
    position: absolute;
    top: 120px;
    left: 560px;
    h3 {
      margin-top: 30px;
    }
    .zhuche {
      float: left;
      margin-left: 45px;
    }
    .chongzhi {
      float: right;
      margin-right: 35px;
    }
    .el-form-item {
      padding: 0;
      width: 350px;
      margin: 20px 50px 20px 0px;
      .el-select {
        margin-left: 0px;
      }
    }
  }
}
</style>